//
//  CardHeadView.swift
//  KaMengApplication
//
//  Created by DUONIU_MAC on 2018/8/31.
//  Copyright © 2018年 DUONIU_MAC. All rights reserved.
//

import UIKit

@objc protocol CardHeadViewDelegate {
    @objc optional func selectType(title:String)
}

class CardHeadView: UIView {
    
    fileprivate let cellId = "cardHeadCell"
    weak var cardDelegate : CardHeadViewDelegate?
    
    var dataArray = [cardArtifact,liftingCheats,progressInquiry,creditCardActivation,creditCardTopic]
    var images = [#imageLiteral(resourceName: "bksq"),#imageLiteral(resourceName: "temj"),#imageLiteral(resourceName: "jdcx"),#imageLiteral(resourceName: "xykjh"),#imageLiteral(resourceName: "xykzt")]
    
    fileprivate let moveView = UIView()

    init() {
        super.init(frame: .zero)
        
        self.backgroundColor = UIColor.white
        
        let layout = UICollectionViewFlowLayout()
        layout.scrollDirection = .horizontal
        layout.minimumLineSpacing = 0
        layout.itemSize = CGSize.init(width: ScreenW/4, height: RATIO_H(maxNum: 90))
        
        let collectionView = UICollectionView.init(frame: .zero, collectionViewLayout: layout)
        collectionView.bounces = false
        collectionView.showsHorizontalScrollIndicator = false
        collectionView.backgroundColor = UIColor.clear
        collectionView.delegate = self
        collectionView.dataSource = self
        collectionView.register(CardHeadCell.self, forCellWithReuseIdentifier: cellId)
        self.addSubview(collectionView)
        
        collectionView.snp.makeConstraints { (make) in
            make.edges.equalToSuperview().inset(UIEdgeInsets.zero)
        }
        
        moveView.backgroundColor = UIColor.moveColor()
        self.addSubview(moveView)
        
        moveView.snp.makeConstraints { (make) in
            make.left.equalToSuperview()
            make.bottom.equalToSuperview()
            make.height.equalTo(RATIO_H(maxNum: 5))
            make.width.equalTo(ScreenW/4 * 3)
        }
        
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
}

extension CardHeadView : UICollectionViewDelegate,UICollectionViewDataSource {
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return dataArray.count
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: cellId, for: indexPath) as? CardHeadCell
        cell?.titleLabel.text = dataArray[indexPath.row]
        cell?.headImageView.image = images[indexPath.row]
        return cell!
    }
    
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        collectionView.deselectItem(at: indexPath, animated: true)
        self.cardDelegate?.selectType!(title: dataArray[indexPath.row])
    }
    
    func scrollViewDidScroll(_ scrollView: UIScrollView) {
        
        moveView.frame.origin.x = scrollView.contentOffset.x
    }
    
}
